#!encoding: utf-8
from django.db import models
from django.contrib.auth.models import User
from mptt.models import MPTTModel, TreeForeignKey
import datetime

#####################CUSTOMER RELATED STAFF#######################################


class News(models.Model):
    date = models.datetime


class Customer(models.Model):
    user_type_choice = (
      (1,u'Юридическое лицо'),
      (0,u'Физическое  лицо'))

    payment_type_choice = (
      (0,"наличными"      ),
      (1,"безналичными"   ),
      (2,"через сберкассу"))

    
    fio      = models.CharField(max_length=140,   blank=True  )


    phone    = models.CharField(max_length=12,    )
    email    = models.CharField(max_length=140)
    passw    = models.CharField(max_length=12)
    address  = models.TextField()
    pay_type = models.SmallIntegerField(choices=user_type_choice)
    is_vip   = models.NullBooleanField()

 #   is_company   = models.SmallIntegerField(choices=user_type_choice)
 #   company_info = models.ForeignKey('Company',blank=True, null=True)


    def __unicode__(self):
        return self.fio

class Company(models.Model):

    company_name    = models.CharField(max_length=1024)      #["_nonempty_LEGAL_PERSON23"]
    company_address = models.CharField(max_length=1024)      #["_nonempty_ADDRESS23"]
    bank_name       = models.CharField(max_length=1024)      #["_nonempty_BANK_NAME23"]
    bik_number      = models.CharField(max_length=1024)      #["_nonempty_BIK23"]

    accaunt_two     = models.IntegerField()        #["_nonempty__number_K_ACCOUNT23"]    
    okpo_number     = models.IntegerField()        #["_nonempty__number_CODE_OKPO23"]
    okved_number    = models.IntegerField()        #["_nonempty__number_CODE_OKONH23"]
    inn_number      = models.IntegerField()        #["_nonempty__number_INN23"]
    acaunt_one      = models.IntegerField()        #["_nonempty__number_R_ACCOUNT23"]




class Customer_Address(models.Model):
    customer = models.ForeignKey('Customer')
    name     = models.CharField(max_length=1000)
    phone    = models.CharField(max_length=20)
    fax      = models.CharField(max_length=20)
    address  = models.TextField()
    comment  = models.TextField(blank=True)

    def __unicode__(self):
        return self.name

    def commit(self):
        if not self.name and self.phone:
            self.name = self.customer.name
            self.phone= self.customer.phone


class Order(models.Model):
    customer  = models.ForeignKey('Customer')
    address   = models.ForeignKey('Customer_Address')
    book      = models.ForeignKey('Book')

    date      = models.DateField(auto_now=True)
    comment   = models.TextField()



############################BOOK RELATED STAFF###########################
class Category(MPTTModel):
    name        = models.CharField(max_length=50)
    legacy_url  = models.CharField(max_length=100,blank=True) 
    legacy_id   = models.SmallIntegerField(max_length=3,blank=True)

    parent = TreeForeignKey('self', null=True, blank=True, related_name='children')


    class MPTTMeta:
        order_insertion_by = ['name']

    def __unicode__(self):
        return self.name


class Book(models.Model):
    storage_choices =(
      (0,u'баум' ),
      (1,u'бг'   ),
      (2,u'вднх' ),
      (3,u'дк'   ),
      (4,u'екат' ),
      (5,u'мтрп' ))

    owner_choices = (
      (1,1),
      (2,2),
      (3,3),
      (4,4),
      (5,5),
      (7,7))
    status_choices =(
      (0,u'продан'    ),
      (1,u'отложен'   ),
      (2,u'в продажу' ))
    resolution_choices = (
      (0,u'Снять публикацию'),
      (1,u'Опубликовать'))

    

    author =      models.CharField(max_length=1000, blank=True )
    name =        models.CharField(max_length=1000, blank=True )
    place=        models.CharField(max_length=100 , blank=True )
    publisher =   models.CharField(max_length=1000, blank=True )
    year =        models.CharField(max_length=10  , blank=True )
    pages =       models.CharField(max_length=1000, blank=True )
    size  =       models.CharField(max_length=1000, blank=True )
    cover =       models.CharField(max_length=1000, blank=True )
    description = models.TextField(                 blank=True )
    safety =      models.TextField(                 blank=True )
    price  =      models.IntegerField(              blank=False)

    status =      models.SmallIntegerField(choices=status_choices)
    storage =     models.SmallIntegerField(choices=storage_choices)
    date    =     models.DateField()
    resolution  = models.SmallIntegerField(choices=resolution_choices)
    owner       = models.SmallIntegerField(choices=owner_choices)

    category  = models.ForeignKey('Book',blank=True,related_name='books')

    def __unicode__(self):
        return self.name+u" "+self.author
 

class Picture(models.Model):

    book = models.ForeignKey('Book')

    thumbnail    = models.ImageField(upload_to='thumbnails/')
    main         = models.ImageField(upload_to='full_images/')














